package org.hzero.order.api.controller.v1;

import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import org.hzero.order.app.service.OrderService;
import org.hzero.order.config.SwaggerTags;
import org.hzero.order.domain.entity.dto.OrderDTO;
import org.hzero.order.domain.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.domain.Page;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author xiuiqng.Yang-Estelle-양수청
 * @date 2019/8/5 16:23
 */

/**
 * API接口
 */
@Api(tags = SwaggerTags.ORDER)
@RestController("orderController.v1")
@RequestMapping("/v1/{organizationId}/orders")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderRepository orderRepository;

    @ApiOperation(value = "多条件查询订单")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping
    public ResponseEntity<Page<OrderDTO>> queryOrderList(
            String companyName,
            String customerName,
            String orderNumber,
            String itemCode,
            String orderStatus,
            PageRequest pageRequest) {
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setCompanyName(companyName).setCustomerName(customerName)
                .setOrderNumber(orderNumber).setItemCode(itemCode).setOrderStatus(orderStatus);
        return Results.success(orderRepository.pageOrder(orderDTO, pageRequest));
    }

    @ApiOperation(value = "通过订单编号查看订单")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @GetMapping("/{orderNumber}")
    public ResponseEntity<Page<OrderDTO>> queryOrderByOrderNumber(
            @PathVariable("orderNumber") String orderNumber,
            PageRequest pageRequest){
        return Results.success(orderRepository.pageOrderDetail(orderNumber, pageRequest));
    }


    @ApiOperation(value = "根据订单状态显示描述")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping("/desc")
    public ResponseEntity<String> queryOrderDescByStatus(
            @RequestParam String orderNumber,
            @RequestParam String orderStatus){
        return Results.success(orderService.selectOrderDesc(orderNumber,orderStatus));
    }

    @ApiOperation(value = "根据订单状态名查询状态值")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @PostMapping("/status")
    public ResponseEntity<String> queryOrderStatus(
            @RequestParam String orderStatus){
        return Results.success(orderService.chooseStatus(orderStatus));
    }

}